package com.shell.governor.modules.service.impl;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.file.service.PubFileService;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.modules.dao.PubParameterDao;
import com.shell.governor.modules.dao.PubParameterRcdDao;
import com.shell.governor.modules.po.PubParameter;
import com.shell.governor.modules.po.PubParameterRcd;
import com.shell.governor.modules.service.PubParameterService;
import com.shell.governor.modules.vo.ParameterVo;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PubParameterServiceImpl extends PersistentServiceImpl<PubParameter> implements PubParameterService {
	@Autowired
	private PubParameterRcdDao pubParameterRcdDao;
	@Autowired
	private PubParameterDao pubParameterDao;
	@Autowired
	private PubFileService pubFileService;

	public PubParameter getParamByCode(String paramCode) {
		PubParameter param = new PubParameter();
		param.setParmCode(paramCode);
		return (PubParameter) selectOne(param);
	}

	public ParameterVo getParameter(String bizType, String paramCode, String parmRlatId, String editorId) {
		List<ParameterVo> parameterVos = this.pubParameterDao.selectParameters(bizType.split(","), parmRlatId,
				paramCode, editorId);
		if ((parameterVos != null) && (parameterVos.size() > 0)) {
			return (ParameterVo) parameterVos.get(0);
		}
		return null;
	}

	public void updateParam(ParameterVo parameter) {
		PubParameter queryParam = new PubParameter();
		queryParam.setParmRlatId(parameter.getParmRlatId());
		queryParam.setParmCode(parameter.getParmCode());
		if (!StringUtil.empty(parameter.getEditor())) {
			queryParam.setEditor(parameter.getEditor());
		}
		PubParameter param = (PubParameter) this.pubParameterDao.selectOne(queryParam);
		if (param == null) {
			param = new PubParameter();
			param.setParmId(SystemUtil.getUUID());
			if (StringUtil.empty(parameter.getEditor())) {
				param.setEditor(CommonDataUtil.getUserId());
			} else {
				param.setEditor(parameter.getEditor());
			}
			param.setEditTime(DateUtil.getSystemDateTime());
			param.setParmCode(parameter.getParmCode());
			param.setParmRlatId(parameter.getParmRlatId());
			param.setParmValue(parameter.getParmValue());
			this.pubParameterDao.insert(param);
		} else {
			PubParameterRcd parameterRcd = new PubParameterRcd();
			BeanUtils.copyProperties(param, parameterRcd);
			parameterRcd.setRcdId(SystemUtil.getUUID());
			this.pubParameterRcdDao.insert(parameterRcd);

			param.setParmValue(parameter.getParmValue());
			if (StringUtil.empty(parameter.getEditor())) {
				param.setEditor(CommonDataUtil.getUserId());
			} else {
				param.setEditor(parameter.getEditor());
			}
			param.setEditTime(DateUtil.getSystemDateTime());
			this.pubParameterDao.updateByPrimaryKey(param);
		}
		if (("File".equals(parameter.getParmType())) && (!StringUtil.emptyAndNull(parameter.getExtension()))) {
			this.pubFileService.setRelateId(param.getParmValue(), param.getParmRlatId(), parameter.getExtension());
		}
	}

	public List<ParameterVo> getParameterList(String bizType, String parmRlatId, String editorId) {
		if ((StringUtil.trimEmpty(parmRlatId)) && ("0".equals(bizType))) {
			parmRlatId = bizType;
		}
		return this.pubParameterDao.selectParameters(bizType.split(","), parmRlatId, "", editorId);
	}

	public PubParameter getParameter(String parmCode, String parmRlatId, String editorId) {
		if ((StringUtil.emptyAndNull(parmCode)) || (StringUtil.emptyAndNull(parmRlatId))) {
			throw new UnCheckMsgException("������������");
		}
		PubParameter pp = new PubParameter();
		pp.setParmRlatId(parmRlatId);
		pp.setParmCode(parmCode);
		if (StringUtil.notTrimEmpty(editorId)) {
			pp.setEditor(editorId);
		}
		return (PubParameter) this.pubParameterDao.selectOne(pp);
	}

	public int deleteFileParams(String id) {
		int count = this.pubFileService.delete(id);
		PubParameter parameter = new PubParameter();
		parameter.setParmValue(id);
		count += this.pubParameterDao.delete(parameter);
		return count;
	}
}
